from threading import Thread,Lock

#  需求：采用两个线程，分别对g_num累加100万次
# 创建一把互斥锁
lock = Lock()
g_num = 0


def sum_num1():
    with lock:
        for i in range(10000000):
            global g_num
            g_num += 1
        print(f'线程1累加之后的结果{g_num}')


def sum_num2():
    with lock:
        for i in range(10000000):
            global g_num
            g_num += 1
        print(f'线程2累加之后的结果{g_num}')


if __name__ == '__main__':
    t1 = Thread(target=sum_num1)
    t2 = Thread(target=sum_num2)

    t1.start()
    t2.start()
